﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.product
{
    public partial class productproperty_edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                string classValue = ChangeHope.WebPage.PageRequest.GetString("classValue");
                if (classValue != "")
                {
                    ViewState["ClassValue"] = classValue;
                }
                string propertyId = ChangeHope.WebPage.PageRequest.GetString("propertyId");
                if (propertyId != "")
                {
                    ViewState["PropertyId"] = propertyId;
                }
                else
                {
                    ChangeHope.WebPage.Script.GoBack();
                }
                this.HyperLink2.NavigateUrl = "productproperty_list.aspx?classValue=" + ViewState["ClassValue"] + "&id=" + propertyId;
                if (ChangeHope.WebPage.PageRequest.GetInt("id") > 0)
                {
                    BindInfo(ChangeHope.WebPage.PageRequest.GetInt("id"), Convert.ToInt32(ViewState["PropertyId"]));
                }
                InitWebControl();
            }
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Save(); 
        }
        /// <summary>
        /// 验证
        /// </summary>
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.WebControl.Validate(this.txtName, "输入商品的属性名称,您再添加商品时会自动加载该属性.", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtSort, "输入数字将作为显示的排列顺序", "isint", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        protected void BindInfo(int id,int productTypeId)
        {
            YXShop.BLL.Product.ProductType bll = new YXShop.BLL.Product.ProductType();
            YXShop.Model.Product.ProductTypeProperty model = bll.GetProductPropertyModel(id, productTypeId);
            if (model != null)
            {
                this.txtName.Text = model.PropertyName;
                this.txtDataValue.Text = model.PropertyValue;
                this.txtSort.Text = model.Sort.ToString();
                this.ddlType.SelectedValue = model.PropertyType.ToString();
                this.ddlClass.SelectedValue = model.PropertyClass.ToString();
                if (model.PropertyType.ToString() == "4" || model.PropertyType.ToString() == "5")
                {
                    this.trDatavalue.Style.Value = "display:none";
                }
                ViewState["ID"] = model.ID;
            }
 
        }
        /// <summary>
        /// 获取分类名称
        /// </summary>
        /// <param name="strId"></param>
        /// <returns></returns>
        protected string ProductClassName(string strId)
        {
            string reStr = string.Empty;
            if (!string.IsNullOrEmpty(strId))
            {
                YXShop.BLL.Product.Productclass dll = new YXShop.BLL.Product.Productclass();
                DataTable dt = dll.GetMoreThanClassName(strId);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (!string.IsNullOrEmpty(reStr))
                    {
                        reStr = reStr + "," + dt.Rows[i]["name"].ToString();
                    }
                    else
                    {
                        reStr = dt.Rows[i]["name"].ToString();
                    }
                }
            }
            return reStr;
        }
        private void Save()
        {
            YXShop.BLL.Product.ProductType bll = new YXShop.BLL.Product.ProductType();
            YXShop.Model.Product.ProductTypeProperty model = new YXShop.Model.Product.ProductTypeProperty();
            model.PropertyName = this.txtName.Text.Trim();
            model.PropertyType = ChangeHope.Common.StringHelper.StringToInt(this.ddlType.SelectedValue);
            model.PropertyValue = this.txtDataValue.Text;
            model.Sort = ChangeHope.Common.StringHelper.StringToInt(this.txtSort.Text);
            model.PropertyEnglishName = "";
            model.TypeID = Convert.ToInt32(ViewState["PropertyId"]);
            model.PropertyClass = Convert.ToInt32(this.ddlClass.SelectedValue);
            if (ViewState["ID"] == null)
            {
                bll.AddProductProperty(model);
                ChangeHope.WebPage.BasePage.PageRight("添加成功，您还可以继续添加。", "productproperty_edit.aspx?classValue=" + ViewState["ClassValue"] + "&propertyId=" + ViewState["PropertyId"]);
            }
            else
            {
                model.ID = Convert.ToInt32(ViewState["ID"]);
                bll.UpdateProductProperty(model, Convert.ToInt32(ViewState["PropertyId"]));
                ChangeHope.WebPage.BasePage.PageRight("修改成功。", "productproperty_list.aspx?classValue=" + ViewState["ClassValue"] + "&id=" + ViewState["PropertyId"]);
            }
        }
    }
}
